import { Breadcrumb, Table, message } from 'antd';
import React, { FC } from 'react';
import { Utils } from 'lfun-bg-comp';
import _ from 'lodash';
import { PageContainer } from '@/component/page-container';
import { PAGINATIONCONFIG } from '@/constant/page';
import { Major, allLv1Major, allLv2Major, searchMajor, updateMajor, allLv3Major } from '@/service/unit/major';
import { get_stoge_pz, set_stoge_pz } from '@/utils';

// const mockdata = [
//   {
//     name: '农林牧渔大类',
//     code: '51',
//     selected: true,
//     children: [
//       {
//         name: '林业技术类',
//         code: '5101',
//         selected: true,
//       },
//       {
//         name: '农业技术类',
//         code: '5102',
//         selected: true,
//       },
//       {
//         name: '畜牧业类',
//         code: '5103',
//         selected: true,
//       },
//       {
//         name: '水产养殖类',
//         code: '5104',
//         selected: true,
//       },
//       {
//         name: '农林管理类',
//         code: '5105',
//         selected: true,
//       },]
//   },
//   {
//     name: '交通运输大类',
//     code: '52',
//     selected: true,
//   },
//   {
//     name: '生化与药品大类',
//     code: '53',
//     selected: true,
//   },
//   {
//     name: '资源开发与测绘大类',
//     code: '54',
//     selected: true,
//   },
//   {
//     name: '材料与能源大类',
//     code: '55',
//     selected: false,
//   },
//   {
//     name: '土建大类',
//     code: '56',
//     selected: false,
//   },
//   {
//     name: '水利大类',
//     code: '57',
//     selected: false,
//   },
//   {
//     name: '制造大类',
//     code: '58',
//     selected: true,
//   },
//   {
//     name: '电子信息大类',
//     code: '59',
//     selected: true,
//   },
//   {
//     name: '环保、气象与安全大类',
//     code: '60',
//     selected: true,
//   },
//   {
//     name: '轻纺食品大类',
//     code: '61',
//     selected: true,
//   },
// ];
const storage_page_size_key = 'major-idx-page-size';

const Groups: FC = () => {
  const [value, setValue] = React.useState([]);
  const [total, setTotal] = React.useState(0);
  const [pageSize, setPageSize] = React.useState(get_stoge_pz(50));
  const [pageIndex, setPageIndex] = React.useState(1);
  const [selectedList, setSelectedList] = React.useState<any[]>([]);
  const [allkeys, setAllkeys] = React.useState<string[]>([]);

  const pageChange = (pageIndex, pageSize) => {
    set_stoge_pz(pageSize);
    setPageIndex(pageIndex);
    setPageSize(pageSize);
  };
  const init = async () => {
    const res = await allLv1Major();
    if (res.success) {
      const res2 = await allLv2Major();
      if (res2.success) {
        const res3 = await allLv3Major();
        if (res3.success) {
          setTotal(res.data.items.length);
          const selectedVal = await searchMajor(Utils.getProps('schoolInfo')?.school_id);
          setSelectedList(selectedVal.data.items);
          const list = res?.data?.items?.map(item => {
            allkeys.push(item.enum_value);
            return {
              ...item,
              key: item.enum_value,
              selected: _.findIndex(selectedVal.data.items, { major_no: item.enum_value }) !== -1,
              children: res2.data.items
                .filter(subitem => subitem.parent_id === item.enum_value)
                .map(subitem => {
                  //allkeys.push(subitem.enum_value);
                  return {
                    ...subitem,
                    key: subitem.enum_value,
                    selected: _.findIndex(selectedVal.data.items, { major_no: subitem.enum_value }) !== -1,
                    children: res3.data.items
                      .filter(el => el.parent_id === subitem.enum_value)
                      .map(el => {
                        //allkeys.push(subitem.enum_value);
                        return {
                          ...el,
                          key: el.enum_value,
                          selected: _.findIndex(selectedVal.data.items, { major_no: el.enum_value }) !== -1,
                        };
                      }),
                  };
                }),
            };
          });
          setAllkeys([...allkeys]);
          setValue(list);
        }
      }
    }
  };
  React.useEffect(() => {
    init();
  }, []);

  const columns = [
    {
      title: '学科名称',
      dataIndex: 'description',
      key: 'description',
      width: 150,
    },
    {
      title: '代码',
      dataIndex: 'enum_value',
      key: 'enum_value',
      width: 100,
    },
    {
      title: '选择',
      dataIndex: 'selected',
      key: 'selected',
      width: 100,
      render: (text: any, record: any) => {
        return (
          <input
            type="checkbox"
            checked={text}
            onChange={async () => {
              console.log(record, '==========>>>', selectedList);

              const up_selectedList = [...selectedList];
              if (!text) {
                const params: Major = {
                  major_name: record.description,
                  major_no: record.enum_value,
                };
                up_selectedList?.push(params);
              } else {
                _.remove(up_selectedList, item => item.major_no === record.enum_value);
                _.remove(up_selectedList, item => item.parent_id === record.enum_value);
              }
              record?.children?.forEach(element => {
                if (!text) {
                  const params: Major = {
                    major_name: element.description,
                    major_no: element.enum_value,
                  };
                  up_selectedList?.push(params);
                } else {
                  _.remove(up_selectedList, item => item.major_no === element.enum_value);
                  _.remove(up_selectedList, item => item.parent_id === element.enum_value);
                }
              });
              ///const course = _.find(value, { course_no: record.code });
              const res = await updateMajor(Utils.getProps('schoolInfo')?.school_id, up_selectedList);
              if (res.success) {
                setSelectedList(up_selectedList);
                const val = value.map(v => ({
                  ...v,
                  selected: _.findIndex(up_selectedList, { major_no: v.enum_value }) !== -1,
                  children: v.children.map(subitem => {
                    return {
                      ...subitem,
                      selected: _.findIndex(up_selectedList, { major_no: subitem.enum_value }) !== -1,
                    };
                  }),
                }));
                setValue(val);
              } else {
                message.error('修改失败');
              }
            }}
          />
        );
      },
    },
  ];

  return (
    <PageContainer header={<Breadcrumb items={[{ title: '中等职业技术学校专业管理' }]} />}>
      <Table
        dataSource={value}
        columns={columns}
        expandable={{ defaultExpandedRowKeys: allkeys }}
        scroll={{ x: 'max-content' }}
        pagination={{ ...PAGINATIONCONFIG, total, pageSize, current: pageIndex, onChange: pageChange }}
      />
    </PageContainer>
  );
};

export default Groups;
